<?php
require_once("./include/db_info.inc.php");
require_once("./bm-header.php");
require_once("./include/funcs.php");
$TeamBool = 0;
$is_admin = isset($_SESSION['administrator']) || isset($_SESSION[$BM_ADMIN]);
if (isset($_GET['cid']))
{
	$cid=intval($_GET['cid']);
	$view_cid=$cid;
	// check contest valid
	$sql="SELECT * FROM `bm_contest` WHERE `bm_c_id`='$cid' ";
	$result=mysql_query($sql);
	$rows_cnt=mysql_num_rows($result);
	$contest_ok=true;
	if ($rows_cnt==0)
	{
		mysql_free_result($result);
		echo "<h2>No Such Contest!</h2><br/><br/>";
		echo '<img src="./images/404.jpg">';
		require_once("bm-footer.php");
		exit(0);
	}
	else
	{
		$row=mysql_fetch_object($result);
		$now=time();
		$Ckind=$row->bm_c_kind;
		$SRtime=strtotime($row->bm_c_startreg);
		$ERtime=strtotime($row->bm_c_endreg);
		$bm_c_defunct=$row->bm_c_defunct;
		$view_description=$row->bm_c_description;
		$view_title= $row->bm_c_title;
		$view_start_time=$row->bm_c_start;
		$view_end_time=$row->bm_c_end;
		$view_SRtime=$row->bm_c_startreg;
		$view_ERtime=$row->bm_c_endreg;
		if ($row->bm_c_defunct=='Y' || !isset($_SESSION['user_id'])
			|| $now < $SRtime || $now > $ERtime) $contest_ok=false;
		if (isset($_SESSION['administrator']) || isset($_SESSION[$BM_ADMIN])) $contest_ok=true;
	}
	if (!$contest_ok && $bm_c_defunct == 'Y')
	{
		echo "<h2>No Such Contest!</h2><br/><br/>";
		echo '<img src="./images/404.jpg">';
		require_once("bm-footer.php");
		exit(0);
	}
/**************************************************/
/********************处理报名信息********************/
	if($contest_ok)
	{
		$bm_c_id=$cid;
		$bm_t_ojid=$_SESSION['user_id'];
		$sql="SELECT * FROM `bm_team` WHERE `bm_c_id`=$bm_c_id AND `bm_t_ojid`='$bm_t_ojid'";
		$result=mysql_query($sql);
		global $TeamBool;
		if($is_admin) $TeamBool=0;
		else $TeamBool=mysql_num_rows($result);
		$bm_t_id;
		if($TeamBool)//该OJID已注册比赛，获取注册信息
		{
			//获取队伍id
			$row=mysql_fetch_assoc($result);
			$bm_t_id=$row['bm_t_id'];
			mysql_free_result($result);
			//处理个人赛组队赛转换的队员个数
			if($Ckind == '1')//个人赛
			{
				//即使是个人赛，个人信息也绑定到一个队伍
				//删除由于组队赛转为个人赛造成的多队员情况
				$sql="DELETE FROM `bm_stu` WHERE `bm_t_id`=$bm_t_id AND (`bm_s_123`=1 OR `bm_s_123`=2)";
				mysql_query($sql) or die(mysql_error());
			}
			else//组队赛
			{
				//增加由于个人赛转为组队赛造成的少队员情况
				for($i = 1; $i < 3; $i ++)
				{
					$sql="
					INSERT INTO `bm_stu`(`bm_t_id`, `bm_s_123`) 
					SELECT '$bm_t_id','$i' 
					FROM  dual 
					WHERE not exists (SELECT * FROM `bm_stu` WHERE `bm_t_id`=$bm_t_id AND `bm_s_123`=$i)";
					mysql_query($sql) or die(mysql_error());
				}
			}
			/**************************************************/
			//读取已注册信息
			$sql="
			SELECT * FROM `bm_team`, `bm_stu` 
			WHERE `bm_team`.`bm_t_id`=`bm_stu`.`bm_t_id` AND `bm_team`.`bm_t_id`='$bm_t_id'
			ORDER BY `bm_s_123` ASC
			";
			$result=mysql_query($sql);
			if($Ckind == '1')//个人赛
			{
				$row=mysql_fetch_assoc($result);
				$bm_t_name=$row['bm_t_name'];
				$bm_t_coach=$row['bm_t_coach'];
				$bm_s_number[0]=$row['bm_s_number'];
				$bm_s_name[0]=$row['bm_s_name'];
				$bm_s_school[0]=$row['bm_s_school'];
				$bm_s_class[0]=$row['bm_s_class'];
				$bm_s_phone[0]=$row['bm_s_phone'];
				$bm_s_mail[0]=$row['bm_s_mail'];
				$bm_s_sex[0]=$row['bm_s_sex'];
				$bm_s_codelang[0]=$row['bm_s_codelang'];
			}
			else//组队赛
			{
				for($i = 0; $i < 3; $i ++)
				{
					$row=mysql_fetch_object($result);
					$bm_t_name=$row->bm_t_name;
					$bm_t_coach=$row->bm_t_coach;
					$bm_s_number[$i]=$row->bm_s_number;
					$bm_s_name[$i]=$row->bm_s_name;
					$bm_s_school[$i]=$row->bm_s_school;
					$bm_s_class[$i]=$row->bm_s_class;
					$bm_s_phone[$i]=$row->bm_s_phone;
					$bm_s_mail[$i]=$row->bm_s_mail;
					$bm_s_sex[$i]=$row->bm_s_sex;
					$bm_s_codelang[$i]=$row->bm_s_codelang;
				}
			}
		}
		/**************************************************/
		//报名，得到提交表单
		if (isset($_POST['submit']))
		{	
			$vcode=trim($_POST['vcode']);
	    if($BM_VCODE&&($vcode!= $_SESSION["vcode"]||$vcode==""||$vcode==null) ){
			echo "<script language='javascript'>\n";
			echo "alert('Verify Code Wrong!');\n";
			echo "history.go(-1);\n";
			echo "</script>";
			exit(0);
	    }
			require_once("./include/check_post_key.php");
			require_once("./include/funcs.php");
			$bm_t_name=myMQpro($_POST['bm_t_name']);
			$bm_t_coach=myMQpro($_POST['bm_t_coach']);
			if ($TeamBool)//该OJID已注册该比赛
			{
				//更新队伍信息
				$sql="UPDATE `bm_team` set `bm_t_name`='$bm_t_name',`bm_t_coach`='$bm_t_coach' WHERE `bm_t_id`=$bm_t_id";
				mysql_query($sql) or die(mysql_error());
			}
			else//该OJID未注册该比赛
			{
				//插入队伍
				$sql="INSERT INTO `bm_team`(`bm_t_name`,`bm_c_id`,`bm_t_ojid`,`bm_t_coach`)
					VALUES('$bm_t_name','$bm_c_id','$bm_t_ojid','$bm_t_coach')";
				mysql_query($sql) or die(mysql_error());
				$bm_t_id=mysql_insert_id();
			}
			/**************************************************/
			//插入或更新信息表
			if($Ckind == '1')//个人赛
			{
				$bm_s_number[0]=myMQpro($_POST['bm_s_number0']);
				$bm_s_name[0]=myMQpro($_POST['bm_s_name0']);
				$bm_s_school[0]=myMQpro($_POST['bm_s_school0']);
				$bm_s_class[0]=myMQpro($_POST['bm_s_class0']);
				$bm_s_phone[0]=myMQpro($_POST['bm_s_phone0']);
				$bm_s_mail[0]=myMQpro($_POST['bm_s_mail0']);
				$bm_s_sex[0]=myMQpro($_POST['bm_s_sex0']);
				$bm_s_codelang[0]=myMQpro($_POST['bm_s_codelang0']);
				if ($TeamBool)//该OJID已注册该比赛
				{
					$sql="UPDATE `bm_stu` set `bm_s_number`='$bm_s_number[0]',`bm_s_name`='$bm_s_name[0]',`bm_s_school`='$bm_s_school[0]',`bm_s_class`='$bm_s_class[0]',`bm_s_phone`='$bm_s_phone[0]',`bm_s_mail`='$bm_s_mail[0]',`bm_s_sex`='$bm_s_sex[0]',`bm_s_codelang`='$bm_s_codelang[0]' WHERE `bm_t_id`='$bm_t_id'";
					mysql_query($sql) or die(mysql_error());
				}
				else//该OJID未注册该比赛
				{
					$sql="INSERT INTO `bm_stu`(`bm_s_number`,`bm_s_name`,`bm_s_school`,`bm_s_class`,`bm_s_phone`,`bm_s_mail`,`bm_s_sex`,`bm_s_codelang`,`bm_t_id`,`bm_s_123`)
	VALUES('$bm_s_number[0]','$bm_s_name[0]','$bm_s_school[0]','$bm_s_class[0]','$bm_s_phone[0]','$bm_s_mail[0]','$bm_s_sex[0]','$bm_s_codelang[0]','$bm_t_id','0')";
					mysql_query($sql) or die(mysql_error());
				}
					
			}
			else//组队赛
			{
				for($i = 0; $i < 3; $i ++)
				{
					$bm_s_number[$i]=myMQpro($_POST['bm_s_number'.$i]);
					$bm_s_name[$i]=myMQpro($_POST['bm_s_name'.$i]);
					$bm_s_school[$i]=myMQpro($_POST['bm_s_school'.$i]);
					$bm_s_class[$i]=myMQpro($_POST['bm_s_class'.$i]);
					$bm_s_phone[$i]=myMQpro($_POST['bm_s_phone'.$i]);
					$bm_s_mail[$i]=myMQpro($_POST['bm_s_mail'.$i]);
					$bm_s_sex[$i]=myMQpro($_POST['bm_s_sex'.$i]);
					$bm_s_codelang[$i]=myMQpro($_POST['bm_s_codelang'.$i]);
					if ($TeamBool)//该OJID已注册该比赛
					{
						$sql="UPDATE `bm_stu` set `bm_s_number`='$bm_s_number[$i]',`bm_s_name`='$bm_s_name[$i]',`bm_s_school`='$bm_s_school[$i]',`bm_s_class`='$bm_s_class[$i]',`bm_s_phone`='$bm_s_phone[$i]',`bm_s_mail`='$bm_s_mail[$i]',`bm_s_sex`='$bm_s_sex[$i]',`bm_s_codelang`='$bm_s_codelang[$i]' WHERE `bm_t_id`='$bm_t_id' AND `bm_s_123`=$i";
						mysql_query($sql) or die(mysql_error());
					}
					else//该OJID未注册该比赛
					{
						$sql="INSERT INTO `bm_stu`(`bm_s_number`, `bm_s_name`, `bm_s_school`, `bm_s_class`, `bm_s_phone`, `bm_s_mail`, `bm_s_sex`, `bm_s_codelang`, `bm_t_id`, `bm_s_123`) VALUES('$bm_s_number[$i]','$bm_s_name[$i]','$bm_s_school[$i]','$bm_s_class[$i]','$bm_s_phone[$i]','$bm_s_mail[$i]','$bm_s_sex[$i]','$bm_s_codelang[$i]','$bm_t_id','$i')";
						mysql_query($sql) or die(mysql_error());
					}
						
				}
			}
			header("Location: $BM_HOME/contest.php?cid=".$cid);
		}
		if($TeamBool)
		{
			$bm_t_name=SqlToHtml($bm_t_name);
			$bm_t_coach=SqlToHtml($bm_t_coach);
			for($i = 0; $i < 3; $i ++)
			{
				$bm_s_number[$i]=SqlToHtml($bm_s_number[$i]);
				$bm_s_name[$i]=SqlToHtml($bm_s_name[$i]);
				$bm_s_school[$i]=SqlToHtml($bm_s_school[$i]);
				$bm_s_class[$i]=SqlToHtml($bm_s_class[$i]);
				$bm_s_phone[$i]=SqlToHtml($bm_s_phone[$i]);
				$bm_s_mail[$i]=SqlToHtml($bm_s_mail[$i]);

				$bm_s_sex[$i]=SqlToHtml($bm_s_sex[$i]);
				$bm_s_codelang[$i]=SqlToHtml($bm_s_codelang[$i]);
			}
		}
	/**************************************************/
	}
	
?>
	<center><h3>Contest<?php echo $view_cid;?> - <?php echo $view_title;echo "(".($Ckind=='0'?"组队赛":"个人赛").")"; ?></h3></center>
    <div id="register">
    	<div id="left" height=100%>
        	<div id="ctime">
            	<table>
                	<tr><td>Start Time:</td><td><?php echo $view_start_time;?></td></tr>
                	<tr><td>End Time:</td><td><?php echo $view_end_time;?></td></tr>
                	<tr><td>Start Register:</td><td><font color=green><?php echo $view_SRtime;?></font></td></tr>
                	<tr><td>End Register: </td><td><font color=green><?php echo $view_ERtime;?></font><br /></td></tr>
                	<tr><td>Current Time: </td><td><span id=nowdate ><?php echo date("Y-m-d H:i:s");?></span><br /></td></tr>
                	<tr><td>Status:</td><td>
					<?php
						if_reg_ing($SRtime, $ERtime, $now, $TeamBool);
                    ?></td></tr>
               	</table>
                </div>
            <div id="cinfot">
						<h4>比赛说明：</h4>
            </div>
            <div id="cinfo">
            	<font color=red>学院栏，校内填学院，外校填学校中文全称。<br/>班级填写 专业+班级号 如“计算机0801”</font>	
                <p><?php echo $view_description?></p>
            </div>
		</div>
        <div id="right" align="center">
        <?php
			if($contest_ok)
			{
		?>
              <h4 align=center><font size=4>报名信息表</font></h4>
              <form id="frm" class="validate" method=POST>
                <table id='team_info'>
                    <tr height='30'>
                        <td width='160' align=right><?php If_Star($Ckind); ?>队伍名称:</td>
                        <td colspan='5' align=left><input maxlength='20' <?php If_Require($Ckind); ?> type=text id=bm_t_name name=bm_t_name size=30 value="<?php echo FillForm($bm_t_name); ?>" ></td>
                    </tr>
                    <tr height='30'>
                        <td width='160' align=right>教练（指导老师）姓名:</td>
                        <td colspan='5' align=left><input maxlength='14' type=text name=bm_t_coach size=30 value="<?php echo FillForm($bm_t_coach); ?>" ></td>
                    </tr>
                </table>
                <table id='stu_info'>
                    <tr align=center><td width='50'></td><td width='200'>队员1</td><td width='200'>队员2</td><td width='200'>队员3</td>
                    <tr height='30'>
                        <td width='55' align=right><?php If_Star(0); ?>学号：</td>
                        <?php 
                        for($i = 0; $i < 3; $i ++)
                        {
                            echo "<td align=center><input maxlength='12' ";
                            If_Require($Ckind == 0 ? 0 : ($i < 1 ? 0 : 1));
                            echo " type=text id=bm_s_number$i name=bm_s_number$i size=25 value=\"".FillForm($bm_s_number[$i])."\"></td>";
                        }
                        ?>
                    </tr>
                    <tr height='30'>
                        <td width='55' align=right><?php If_Star(0); ?>姓名：</td>
                        <?php 
                        for($i = 0; $i < 3; $i ++)
                        {
                          echo "<td align=center><input maxlength='14' ";
                          If_Require($Ckind == 0 ? 0 : ($i < 1 ? 0 : 1));
                          echo " type=text id=bm_s_name$i name=bm_s_name$i size=25 value=\"".FillForm($bm_s_name[$i])."\"></td>";
                        }
                        ?>
                    </tr>
                    <tr height='30'>
                        <td width='55' align=right>学院：</td>
                        <?php 
                        for($i = 0; $i < 3; $i ++)
                           echo "<td align=center><input maxlength='20' type=text id=bm_s_school$i name=bm_s_school$i size=25 value=\"".FillForm($bm_s_school[$i])."\"></td>";
                        ?>
                    </tr>
                    <tr height='30'>
                        <td width='55' align=right><?php If_Star(0); ?>班级：</td>
                        <?php 
                        for($i = 0; $i < 3; $i ++)
                        {
                          echo "<td align=center><input maxlength='12' ";
                          If_Require($Ckind == 0 ? 0 : ($i < 1 ? 0 : 1));
                          echo " type=text id=bm_s_class$i name=bm_s_class$i size=25  value=\"".FillForm($bm_s_class[$i])."\"></td>";
                        }
                        ?>
                    </tr>
                    <tr height='30'>
                        <td width='55' align=right><?php If_Star(0); ?>电话：</td>
                        <?php 
                        for($i = 0; $i < 3; $i ++)
                        {
                          echo "<td align=center><input maxlength='15' ";
                          If_Require($i);
                          echo " type=text id=bm_s_phone$i name=bm_s_phone$i size=25 value=\"".FillForm($bm_s_phone[$i])."\"></td>";
                        }
                        ?>
                    </tr>
                    <tr height='30'>
                        <td width='55' align=right>邮箱：</td>
                        <?php 
                        for($i = 0; $i < 3; $i ++)
                            echo "<td align=center><input maxlength='30' type=text id=bm_s_mail$i name=bm_s_mail$i size=25 value=\"".FillForm($bm_s_mail[$i])."\"></td>";
                        ?>
                    </tr>
                    <tr height='30'>
                        <td width='55' align=right>性别：</td>
                        <?php
                        for($i = 0; $i < 3; $i ++)
                            echo "<td align=center><select id=bm_s_sex$i name=bm_s_sex$i><option value=0".($bm_s_sex[$i]?"":" selected").">男</option><option value=1".($bm_s_sex[$i]?" selected":"").">女</option></select></td>";
                        ?>
                    </tr>
                    <tr height='30'>
                        <td width='55' align=right>code：</td>
                        <?php
                        $BmCodeAr=array("C","C++","Pascal","Java","Ruby","Bash","Python","PHP","Perl","C#");
                        for($i = 0; $i < 3; $i ++)
                        {
                            echo "<td align=center><select id=bm_s_codelang$i name=bm_s_codelang$i>";
                            for($j = 0; $j < 10; $j ++)
                                echo "<option value='$BmCodeAr[$j]' ".($BmCodeAr[$j] == $bm_s_codelang[$i] ? "selected":"").">$BmCodeAr[$j]</option>";
                            echo "</select></td>";
                        }
                        ?>
                    </tr>
                </table>
                <?php require_once("./include/set_post_key.php");?>
                <br/>
                <p>
		                <?php if($BM_VCODE){?>
		                <tr><td><?php If_Star(0); ?>验证码:</td>
		                        <td><input class="req" name="vcode" size=4 type=text><img alt="click to change" src=vcode.php onclick="this.src='vcode.php#'+Math.random()"></td>
		                </tr>
		                <?php }?>
                    <input type=submit value=确认报名 name=submit style="height:50px;width:100px;"/>
                <?php 
                if($TeamBool)
								{
								?>		
                    <input type=button value=取消报名 id="cancel_reg" name="qxbm" style="height:50px;width:100px;" <?php echo "onclick='Cancel(".$cid.")'"; ?>/>
               		<script type="text/javascript">
						function Cancel(cid)
						{
							if(confirm("取消报名后您在该比赛的记录将被消除，报名需要重新填写表格。确定要取消报名？"))
							{
								window.location.href = "./cancel.php?cid="+cid;
							}
							else
						  	{
								return false;
							}
						}
					</script>
                <?php
              	}
              	?>
                </p>
                </form>
                <br />
                <?php			
                if($TeamBool)
                {
				    echo "已<font color=red size=5><b>报名成功</b></font>，刷新页面后表格所显示为已记录信息。修改后重新提交即可更新报名信息。";
					echo "<a href=$BM_HOME/results.php?cid=$cid>查看报名结果</a>";
				}
                else
				{
                    echo "未查询到您的报名信息，填写表格后提交即可报名。";
					if($is_admin)
						echo "<a href=$BM_HOME/results.php?cid=$cid>查看报名结果</a>";
				}
                ?>
                <br />
        <?php
        }
		else
		{
			echo "<br /><br /><br />";
			if($now < $SRtime)
				echo "<h2>Register is not started.</h2><br/><br/>";
			else if($now > $ERtime)
				echo "<h2>Register is ended.</h2><br/><br/>";
			else
				echo "<h2>Please login first.</h2><br/><br/>";
		}
		?>
            </div>
        </div>
<?php
	if($Ckind == 1)
	{
?>
	<script language="javascript">
	$(document).ready
	(
		function()
		{
			//禁止填写
			$("#bm_t_name").attr("disabled","disabled");
			$("#bm_s_number1").attr("disabled","disabled");
			$("#bm_s_number2").attr("disabled","disabled");
			$("#bm_s_name1").attr("disabled","disabled");
			$("#bm_s_name2").attr("disabled","disabled");
			$("#bm_s_school1").attr("disabled","disabled");
			$("#bm_s_school2").attr("disabled","disabled");
			$("#bm_s_class1").attr("disabled","disabled");
			$("#bm_s_class2").attr("disabled","disabled");
			$("#bm_s_phone1").attr("disabled","disabled");
			$("#bm_s_phone2").attr("disabled","disabled");
			$("#bm_s_mail1").attr("disabled","disabled");
			$("#bm_s_mail2").attr("disabled","disabled");
			$("#bm_s_sex1").attr("disabled","disabled");
			$("#bm_s_sex2").attr("disabled","disabled");
			$("#bm_s_codelang1").attr("disabled","disabled");
			$("#bm_s_codelang2").attr("disabled","disabled");
		}
	)
	</script>
<?php
	}
}
else
{
?>
	<script>
	var cur=document.getElementById("contest_tab");
	cur.className = "current";
	</script>
	
	<title>Contest List</title>
	<?php
	$sql="SELECT * FROM `bm_contest` WHERE `bm_c_defunct`='N' ORDER BY `bm_c_id` DESC";
	$result=mysql_query($sql);
	$color=false;
	echo "<center><H1>Contest List</H1><table width=90%>";
	echo "<tr class=toprow align=center><td width=10%>ID</td><td width=50%>Name</td><td width=30%>Status</td></tr>";
	while ($row=mysql_fetch_object($result))
	{
		if($color) echo "<tr align=center class=oddrow>";
		else echo "<tr align=center class=evenrow>";
		echo "<td>$row->bm_c_id</td>";
		echo "<td><a href='contest.php?cid=$row->bm_c_id'>$row->bm_c_title</a></td>";
		$start_time=strtotime($row->bm_c_start);
		$end_time=strtotime($row->bm_c_end);
		$start_reg=strtotime($row->bm_c_startreg);
		$end_reg=strtotime($row->bm_c_endreg);
		$now=time();
		// past
		if ($now>$end_reg) echo "<td><font color=green>Ended@$row->bm_c_endreg</font></td>";
		// pending
		else if ($now<$start_reg) echo "<td><font color=blue>Start Register@$row->bm_c_startreg</font></td>";
		// Registering
		else echo "<td><font color=red>Registering!</font></td>";
		echo "</tr>";
		$color=!$color;
	}
	echo "</table></center>";
	mysql_free_result($result);
}
?>

<?php
require_once("bm-footer.php");
?>